package com.atguigu.worktest;

import com.atguigu.bean.MarketingUserBehavior;
import com.atguigu.datasource.MockAppDataSource;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 求：APP通过每个渠道每种行为的数量；
 即通过小米下载了多少，通过华为商城下载了多少，通过ios store下载了多少…

 */
public class Flink02_AppMarketing_ChannelBehaivor {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(2);

        //自定义数据源
        DataStreamSource<MarketingUserBehavior> ds = env.addSource(new MockAppDataSource());

        //对数据进行处理
        ds.map(new MapFunction<MarketingUserBehavior, Tuple2<Tuple2<String,String>,Integer>>() {
            @Override
            public Tuple2<Tuple2<String, String>, Integer> map(MarketingUserBehavior value) throws Exception {

                //这时候传进来的是一个个的对象，那么就需要进行封装
                return Tuple2.of(Tuple2.of(value.getChannel(),value.getBehavior()),1);
            }
        })
                //进行分keyby操作
                .keyBy(new KeySelector<Tuple2<Tuple2<String, String>, Integer>, Tuple2<String,String>>() {
                    @Override
                    public Tuple2<String, String> getKey(Tuple2<Tuple2<String, String>, Integer> value) throws Exception {
                        return value.f0;
                    }
                })
                //进行聚合操作
        .sum(1)
        //打印输出
        .print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
